<template>
  <div class="app-container">
    <el-form size="small" inline>
      <el-form-item label="党建标题">
        <el-input placeholder="请输入" v-model="query.title" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
        <el-button plain icon="el-icon-refresh" @click="handleReset">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="small" @click="dialogVisible = true">新增</el-button>
        <help></help>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="list" border>
      <el-table-column label="序号" align="center" width="50" type="index" fixed="left" />
      <el-table-column label="党建标题" align="center" prop="title" />
      <el-table-column label="党支部名称" align="center" prop="partyName" />
      <el-table-column label="主办单位" align="center" prop="sponsor" />
      <el-table-column label="参与人数" align="center" prop="joinNum" />
      <el-table-column label="党建时间" align="center" prop="activityDate">
        <template slot-scope="scope">{{ Number(scope.row.activityDate) | moment }}</template>
      </el-table-column>
      <el-table-column label="党建地点" align="center" prop="activityPlace" />
      <el-table-column label="教务负责人" align="center" prop="educationAdmin" />
      <el-table-column label="授课负责人" align="center" prop="lecturesAdmin" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="small" type="text" @click="handleInfo(scope.row)">详情</el-button>
          <el-button size="small" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="query.pageNum"
      :limit.sync="query.pageSize"
      @pagination="getList"
    />

    <help-dialog :visible.sync="dialogVisible" title="建党活动" @close="close">
      <el-form size="small" label-width="120px" ref="queryParam" :model="queryParam" :rules="rules">
        <el-row>
          <el-col :span="12">
            <el-form-item label="标题" prop="title">
              <el-input v-model="queryParam.title" placeholder="请输入标题" />
            </el-form-item>
            <el-form-item label="活动时间" prop="activityDate">
              <el-date-picker
                v-model="queryParam.activityDate"
                type="datetime"
                value-format="timestamp"
                placeholder="请选择"
                style="width: 100%"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="党支部名称" prop="partyId">
              <el-select v-model="queryParam.partyId" style="width: 100%" placeholder="请选择党支部名称">
                <el-option
                  v-for="item in partyTypeList"
                  :key="item.partyId"
                  :label="item.name"
                  :value="item.partyId"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="教务负责人" prop="educationAdmin">
              <el-input v-model="queryParam.educationAdmin" placeholder="请输入教务负责人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="参与人数" prop="joinNum">
              <el-input v-model="queryParam.joinNum" placeholder="请输入参与人数" />
            </el-form-item>
            <el-form-item label="活动地点" prop="activityPlace">
              <el-input v-model="queryParam.activityPlace" placeholder="请输入活动地点" />
            </el-form-item>
            <el-form-item label="主办单位" prop="sponsor">
              <el-input v-model="queryParam.sponsor" placeholder="请输入主办单位" />
            </el-form-item>
            <el-form-item label="授课负责人" prop="lecturesAdmin">
              <el-input v-model="queryParam.lecturesAdmin" placeholder="请输入授课负责人" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-col :span="24">
          <el-form-item label="添加附件" prop="file">
            <y-upload :path="[queryParam.file]" limit="1" @onSuccess="onSuccess" />
          </el-form-item>
          <el-form-item label="内容" prop="remark">
            <editor v-model="queryParam.remark" :min-height="192" />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </help-dialog>
  </div>
</template>

<script>
import activityApi from '@/api/party/activity'

export default {
  data() {
    return {
      loading: false,
      list: [],
      total: 0,
      count: 0,
      query: {
        title: '',
        pageNum: 1,
        pageSize: 10
      },
      partyTypeList: [],
      queryParam: {},
      rules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        activityDate: [{ required: true, message: '请选择', trigger: 'blur' }],
        partyId: [{ required: true, message: '请选择党支部名称', trigger: 'blur' }],
        educationAdmin: [{ required: true, message: '请输入教务负责人', trigger: 'blur' }],
        joinNum: [{ required: true, message: '请输入参与人数', trigger: 'blur' }],
        activityPlace: [{ required: true, message: '请输入活动地点', trigger: 'blur' }],
        sponsor: [{ required: true, message: '请输入主办单位', trigger: 'blur' }],
        lecturesAdmin: [{ required: true, message: '请输入授课负责人', trigger: 'blur' }]
      },
      dialogVisible: false
    }
  },
  created() {
    this.getList()
    this.getPartyList()
  },
  methods: {
    getList() {
      this.loading = true
      activityApi.activityList(this.query).then((res) => {
        this.list = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    handleReset() {
      this.query = {
        title: '',
        pageNum: 1,
        pageSize: 10
      }
      this.getList()
    },
    handleInfo(row) {
      activityApi.activityDetail(row.id).then((res) => {
        this.queryParam = res.data

        this.dialogVisible = true
      })
    },
    onSuccess(file) {
      this.queryParam.file = file.router
    },
    getPartyList() {
      activityApi.partyList().then((res) => {
        this.partyTypeList = res.rows
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('确认删除吗？')
        .then(function () {
          return activityApi.delActivity(row.id)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    // 取消按钮
    close() {
      this.queryParam = {}
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['queryParam'].validate((valid) => {
        if (valid) {
          if (this.queryParam.id != null) {
            activityApi.updateActivity(this.queryParam).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.dialogVisible = false
              this.getList()
            })
          } else {
            activityApi.addActivity(this.queryParam).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.dialogVisible = false
              this.getList()
            })
          }
        }
      })
    }
  }
}
</script>
